layui.use(['table', 'util', 'layer', 'laydate'], function(){
    var table = layui.table,
        util = layui.util,
        layer = layui.layer,
        laydate = layui.laydate;

    laydate.render({
        elem: '.startTime'
        ,format: 'yyyy-MM-dd HH:mm:ss' //可任意组合
    });
    laydate.render({
        elem: '.endTime'
        ,format: 'yyyy-MM-dd HH:mm:ss' //可任意组合
    });


    var paperTable = table.render({
        elem: '#testTable'
        ,url:'/paper/selectPaperByPage'
        ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
        ,title: '试卷数据表'
        ,method: 'get'
        ,limits: [10,15,20,30]
        ,limit: 10
        ,id:'testTable'
        ,height: 523
        ,cellMinWidth: 80
        ,cols: [[
            {field:'id', title:'序号', align:'center',type: 'numbers', width: 100, unresize: true, sort: true}
            ,{field:'pname', title:'试卷名',align:'center'}
            ,{field:'pcreatetime', title:'创建时间',align:'center',templet: function (res) {
                    return util.toDateString(res.pcreatetime, "yyyy-MM-dd");
                }}
            ,{field:'pbegintime', title:'开考时间',align:'center', templet: function (res) {
                    return util.toDateString(res.pbegintime, "yyyy-MM-dd HH:mm:ss");
                }}
            ,{field:'pendtime', title:'结束时间',align:'center',templet: function (res) {
                    return util.toDateString(res.pendtime, "yyyy-MM-dd HH:mm:ss");
                }}
            ,{field:'pduration', title:'考试时长',align:'center',templet: function (res) {
                    return res.pduration + "分钟";
                }}
            ,{field:'pscore', title:'总分',align:'center'}
            ,{field:'uname', title:'创建人',align:'center'}
            ,{field:'isExam', title:'是否结束',align:'center', templet: function (res) {
                    var date = new Date()
                        ,begin = new Date(res.pbegintime)
                        ,end = new Date(res.pendtime);
                    if (date < begin){
                        return "<span style='color: #e1e1e1;'>未开始</span>";
                    } else if (date < end){
                        return "<span style='color: #66E8DC;'>正在考试</span>";
                    } else {
                        return "<span style='color: red;'>已结束</span>";
                    }
                }}
            ,{field:'pstatus', title:'试卷状态',align:'center', templet: function (res) {
                    return res.pstatus == 0 ? "<span style='color: red;'>禁用</span>" : "<span style='color: #66E8DC;'>正常</span>";
                }}
            ,{fixed: 'right', title:'操作',align:'center', toolbar: '#barDemo'  }
        ]]
        ,page: true
        ,response: {
            "code": 0,
            "msg": "",
            "count": 1000,
            "data": []
        }
        ,parseData: function(res){ //res 即为原始返回的数据
            return {
                "code": res.data.code, //解析接口状态
                "msg": res.data.msg, //解析提示文本
                "count": res.data.count, //解析数据长度
                "data": res.data.data //解析数据列表
            };
        }

    });

    //头工具栏事件
    table.on('toolbar(test)', function(obj){
        switch(obj.event){
            case 'addPaper':
                layer.open({
                    title: '创建试卷',
                    type: 2,
                    area: ['455px', '667px'],
                    fixed: false, //不固定
                    maxmin: true,
                    content: '/addPaper'
                });
                break;
            case 'reloadPage':
                var pageIndex = $(".layui-laypage-em").next().html();
                paperTable.reload({
                    method:'get'
                    ,page: {
                        curr: pageIndex //重新从第 当前页 开始
                    }
                });
                break;
        };
    });


    $('.do_search').on('click', function (data) {
        // 搜索条件
        var pName=$(".pName").val();
        var uId=$(".uId").val();
        var pBegintime=$(".startTime").val();
        var pEndtime=$(".endTime").val();
        var pStatus=$(".pStatus").val();
        if ((pBegintime == "" && pEndtime != "")){
            layer.tips('不能为空', '.startTime', {
                tips: [1, '#3595CC'],
                time: 4000
            });
            return false;
        }
        if (pBegintime != "" && pEndtime == ""){
            layer.tips('不能为空', '.endTime', {
                tips: [1, '#3595CC'],
                time: 4000
            });
            return false;
        }
        if (pBegintime != "" && pEndtime != ""){
            var  s1 = new Date(pBegintime).getTime();
            var  s2 = new Date(pEndtime).getTime();
            if (s1>s2) {
                layer.tips('起始时间不能大于结束时间', '.endTime', {
                    tips: [1, '#3595CC'],
                    time: 4000
                });
                return false;
            }
        }
        paperTable.reload({
            method:'get'
            ,where: { //设定异步数据接口的额外参数，任意设
                pName: pName,
                uId: uId,
                pBegintime: pBegintime,
                pEndtime: pEndtime,
                pStatus: pStatus
            }
            ,page: {
                curr: 1 //重新从第 1 页开始
            }
        });

    });


    //监听行工具事件
    table.on('tool(test)', function(obj){
        var data = obj.data;
        if(obj.event === 'ban'){
            //获取table表格当前页码
            var pageIndex = $(".layui-laypage-em").next().html()
                ,pstatus = "0"
                ,msg = "是否禁用该试卷？";
            if (data.pstatus == "0"){
                pstatus = "1";
                msg = "是否启用该试卷？";
            }
            layer.confirm(msg,function(index){
                $.ajax({
                    url: '/paper/updatePaperStatus/' + pstatus + '/' + data.pid,
                    type: 'put',
                    data: '',
                    success: function (res) {
                        if (res.code == 200){
                            layer.close(index);
                            paperTable.reload({
                                method:'get'
                                ,page: {
                                    curr: pageIndex //重新从第 当前页 开始
                                }
                            });
                        } else {
                            layer.msg(res.msg);
                        }
                    }
                });
            });
        } else if(obj.event === 'edit'){
            layer.open({
                title: "修改试卷：" + data.pname,
                type: 2,
                area: ['460px', '510px'],
                fixed: false, //不固定
                maxmin: true,
                content: '/updatePaper/' + data.pid
            });


        }
    });
    var _tools = {
        reloadTable : function () {
            var pageIndex = $(".layui-laypage-em").next().html();
            paperTable.reload({
                method:'get'
                ,page: {
                    curr: pageIndex //重新从第 当前页 开始
                }
            });
        }
    };
    window.tools = _tools;
});
